<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Gm extends PNC_Controller {

    public function __Construct() {
        parent::__Construct();
        $this->load->model(array('mod_staff', 'mod_gm', 'mod_option', 'mod_leave_request', 'mod_record'));
    }

    public function home() {
        if ($this->check_session()) {
            $data['title'] = "General Manager";
            $session_id = $this->session->userdata('id_user');
            $data['datas'] = $this->mod_staff->selectdata($session_id);
            $data['total'] = $this->mod_staff->total($session_id);
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    public function leaving() {
        if ($this->check_session()) {
            $data['title'] = "Do Request";
            $data['validator'] = $this->mod_option->get_manage_mail('manager', $this->session->userdata('staff_id'));
            $data['super'] = $this->mod_leave_request->super_type_leave();
            $data['type'] = $this->mod_leave_request->type_leave();
            $data['sub_type'] = $this->mod_leave_request->sub_type_leave();
            $data['sub_type_per'] = $this->mod_leave_request->sub_type_leave_per();
            if ($this->input->post('btn_do_request')) {
                $config = array(
                    array(
                        'field' => 'duration',
                        'label' => 'Time Duration',
                        'rules' => 'trim|required|xss_clean'
                    ),
                    array(
                        'field' => 'reason',
                        'label' => 'Reason',
                        'rules' => 'trim|required|xss_clean'
                    ),
                    array(
                        'field' => 'super_type',
                        'label' => 'Type of Leave',
                        'rules' => 'trim|required|greater_than[0]'
                    ),
                    array(
                        'field' => 'validators',
                        'label' => 'Manager Mail',
                        'rules' => 'trim|required|valid_email'
                    ),
                    array(
                        'field' => 's_date',
                        'label' => ' Start Date',
                        'rules' => 'trim|required|callback_compareDates'
                    ),
                    array(
                        'field' => 'e_date',
                        'label' => 'End Date',
                        'rules' => 'trim|required|callback_compareDates'
                    )
                );
                $this->form_validation->set_rules($config);
                $this->form_validation->set_message('greater_than[0]', 'The field must be select.');
                if ($this->form_validation->run() === FALSE) {
                    $this->session->set_userdata('validate_fail', show_message('Please check your completed form!', 'error'));
                    $this->view_main($data);
                } else {
                    $staff = $this->session->userdata('staff_id');
                    $validator = $this->input->post('validators');
                    $super_type = $this->input->post('super_type');
                    $sub_type = $this->input->post('sub');
                    $sub_sub = $this->input->post('sub_sub');
                    $desc = $this->input->post('desc_up');
                    $work_on = $this->input->post('working_on');
                    $catch_on = $this->input->post('catchup_on');
                    $reason = $this->input->post('reason');
                    $start_date = $this->input->post('s_date');
                    $end_date = $this->input->post('e_date');
                    $hour = $this->input->post('duration');

                    $this->session->set_userdata('reciever_mail', $validator);
                    $this->session->set_userdata('reason_leaving', $reason);

                    $day = ($hour / 8);

                    if ($start_date <= $end_date) {
                        $success_request = '';
                        if ($super_type == 1) {
                            $success_request = PNC_Model::add_record($staff, $validator, $super_type, $sub_type, $sub_sub, '', $reason, $start_date, $end_date, $hour, $day, '', '');
                            if ($success_request > 0) {
                                $this->send_mail_request('gm', 'gm', 'home', 'leaving');
                            } else {
                                $this->session->set_userdata('send_request_erro', show_message('Sending email for do request was failed!', 'error'));
                                $this->view_main($data);
                            }
                        } else if ($super_type == 2) {
                            $success_request = PNC_Model::add_record($staff, $validator, $super_type, '', '', $desc, $reason, $start_date, $end_date, $hour, $day, $work_on, $catch_on);
                            if ($success_request > 0) {
                                $this->send_mail_request('gm', 'gm', 'home', 'leaving');
                            } else {
                                $this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
                                $this->view_main($data);
                            }
                        }
                    } else {
                        $this->session->set_userdata('sent_erro', show_message('Start date must be smaller than end date!', 'error'));
                        $this->view_main($data);
                    }
                }
            } else {
                $this->view_main($data);
            }
        } else {
            redirect('main/');
        }
    }

    /* Show all pending and approve of record */

    public function report() {
        if ($this->check_session()) {
            $this->session->set_userdata("cur_page", $this->uri->segment(3));
            $this->session->set_userdata("pagination", $this->uri->segment(4));
            $data['title'] = 'Record';
            $config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
            $this->session->set_userdata('num_pending', $config['total_rows']);
            $num_per_page = 10;
            /* in case user delete query string */
            if ($this->session->userdata('per_page')) {
                $this->session->unset_userdata('per_page');
            }
            /* get limit per page from query string */
            if ($this->input->get('limit', TRUE)) {
                $this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
                $num_per_page = $this->session->userdata('per_page');
            }
            $config['total_rows'] = ''; // initialize total records
            $config['base_url'] = site_url('gm/report/pending'); // initialize base url
            if ($this->uri->segment(3) === 'pending' OR !$this->uri->segment(3)) {
                $config['base_url'] = site_url('gm/report/pending');
                $config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
            } else if ($this->uri->segment(3) === 'approved') {
                $config['base_url'] = site_url('gm/report/approved');
                $config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 1);
            }
            $config['per_page'] = $num_per_page;
            $config['uri_segment'] = 4;
            $choice = $config["total_rows"] / $config["per_page"];
            $config["num_links"] = round($choice);
            $config['first_link'] = 'First';
            $config['next_link'] = 'Next';
            $config['prev_link'] = 'Pre';
            $config['last_link'] = 'Last';

            $this->pagination->initialize($config); //function to show all pages
            $page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
            $data['leave_record'] = ''; // initalize record seleted
            if ($this->uri->segment(3) === 'pending' OR !$this->uri->segment(3)) {
                /* get leave recrod as pending */
                $data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'pending', $this->session->userdata('staff_id'));
            } else if ($this->uri->segment(3) === 'approved') {
                /* get leave record as approved */
                $data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'approved', $this->session->userdata('staff_id'));
            }
            $data['pagination'] = $this->pagination->create_links();
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    public function get_profile() {
        if ($this->check_session()) {
            $data['title'] = 'Profile';
            $data['get_profile'] = PNC_Model::get_profile($this->session->userdata('staff_id'));
            $this->view_main($data);
        } else {
            redirect('main/');
        }
    }

    /* Change new password */

    public function change_password() {
        if ($this->check_session()) {
            if ($this->input->post('btn_save_change')) {
                $old_pass = $this->input->post('old_pass');
                $new_pass = $this->input->post('new_pass');
                $con_pass = $this->input->post('con_pass');
                $old_pass = md5($old_pass);
                $pass = $this->session->userdata('old_pass');

                if ($old_pass === '' OR $new_pass === '' OR $con_pass === '') {
                    $this->session->set_userdata('input_blank', show_message('Please input all fields!', 'error'));
                    redirect('gm/change_pass');
                } else if (strlen($new_pass) < 5 OR strlen($new_pass) > 15) {
                    $this->session->set_userdata('min_length', show_message('Password must be longer than 5 and shorter than 15 characters!', 'error'));
                    redirect('record/change_pass');
                } else if ($old_pass != $pass) {
                    $this->session->set_userdata('opass_err', show_message('Your old password is not match!', 'error'));
                    redirect('gm/change_pass');
                } else if ($new_pass != $con_pass) {
                    $this->session->set_userdata('npass_err', show_message('New password and confirm password is not match!', 'error'));
                    redirect('gm/change_pass');
                } else {
                    $new_pass = md5($new_pass);
                    PNC_Model::change_password($old_pass, $new_pass, $con_pass, $this->session->userdata('staff_id'));
                    $this->session->set_userdata('change_success', show_message('Changed password successfully!', 'success'));
                    redirect('gm/get_profile');
                }
            }
        } else {
            redirect('main/');
        }
    }

    /* When user complete old or wrong password */

    public function change_pass() {
        $data['title'] = 'Change Password';
        if ($this->input->post('btn_save_change')) {
            $this->change_password();
            redirect('gm/get_profile');
        }
        $this->view_main($data);
    }

    /* Check status of record to approve */

    public function approval() {
        if ($this->check_session()) {
            $this->approval_record($this->uri->segment(5), $this->uri->segment(4), $this->uri->segment(3), 'gm', 'info', 'report', 'own_pending', 'type_pending', 'disapprove');
        } else {
            redirect('main/');
        }
    }

    /* Change personal numbers leaving request */

//	public function info() {
//		if ($this->check_session()) {
//			$this->session->set_userdata("cur_page", $this->uri->segment(3));
//			$this->session->set_userdata("pagination", $this->uri->segment(4));
//			$data['title'] = 'Personal leaving Request';
//			$config['total_rows'] = PNC_Model::personal_leaving($this->session->userdata('staff_id'), 'pending');
//			$this->session->set_userdata('num_own_pending', $config['total_rows']);
//			$num_per_page = 5;
//			/* in case user delete query string */
//			if ($this->session->userdata('per_page')) {
//				$this->session->unset_userdata('per_page');
//			}
//			/* get limit per page from query string */
//			if ($this->input->get('limit', TRUE)) {
//				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
//				$num_per_page = $this->session->userdata('per_page');
//			}
//			$config['total_rows'] = ''; // initialize total records
//			$config['base_url'] = site_url('gm/info/own_pending'); // initialize base url
//			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
//				$config['base_url'] = site_url('gm/info/own_pending');
//				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
//			} else if ($this->uri->segment(3) === 'own_approved') {
//				$config['base_url'] = site_url('gm/info/own_approved');
//				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 1);
//			}
//			$config['per_page'] = $num_per_page;
//			$config['uri_segment'] = 4;
//			$choice = $config["total_rows"] / $config["per_page"];
//			$config["num_links"] = round($choice);
//			$config['first_link'] = 'First';
//			$config['next_link'] = 'Next';
//			$config['prev_link'] = 'Pre';
//			$config['last_link'] = 'Last';
//
//			$this->pagination->initialize($config); //function to show all pages
//			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
//			$data['leave_record'] = ''; // initalize record seleted
//			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
//				/* get leave recrod as pending */
//				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_pending', $this->session->userdata('staff_id'));
//			} else if ($this->uri->segment(3) === 'own_approved') {
//				/* get leave record as approved */
//				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_approved', $this->session->userdata('staff_id'));
//			}
//			$data['pagination'] = $this->pagination->create_links();
//			$this->view_main($data);
//		} else {
//			redirect('main/');
//		}
//	}

    /* Delete before approved from manager or director */

//	public function delete_before_approve() {
//		if ($this->check_session()) {
//			$this->delete_before_approving('gm', 'info');
//		} else {
//			redirect('main/');
//		}
//	}

    /* Delete before approved from manager or director */

    public function delete_before_approve() {
        if ($this->check_session()) {
            $this->delete_before_approving('gm', 'report');
        } else {
            redirect('main/');
        }
    }

    /*
     * Logout Admin
     * @return void
     * @access public
     */

    public function logout_gm() {
//		$this->session->sess_destroy('gm');
        $this->session->set_userdata('logout', show_message('You have been logout!', 'notice'));
        $this->session->unset_userdata('gm');
        redirect("main/");
    }

}

/* End of file gm.php */
/* Location: ./application/controllers/gm.php */